package com.yzmoney.wechat.service;

import com.yzmoney.common.constant.Constants;
import com.yzmoney.wechat.model.reply.ReplyTextMsg;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import java.util.Date;

/**
 * 接受普通消息
 *
 * @author DOVE
 */
@Service
public class WechatCommonMsgReceiveService {
    private static Logger logger = Logger.getLogger(WechatCommonMsgReceiveService.class);

    @Autowired
    private WechatSendMsgService wechatSendMegService;

    @Autowired
    private View jaxb2MarshallingView;

    public ModelAndView receiveMsg(Document docXml) {
        ModelAndView mav = null;
        Element rootElement = docXml.getRootElement();
        Element msgTypeElement = rootElement.element("MsgType");

        if (msgTypeElement.getTextTrim().equals("text")) {
            mav = receiveTextMsg(rootElement);
        } else if (msgTypeElement.getTextTrim().equals("image")) {
            mav = receiveImageMsg(rootElement);
        }
        return mav;
    }

    /**
     * 回复 接收到的文本消息
     *
     * @param rootElement
     * @return
     */
    private ModelAndView receiveTextMsg(Element rootElement) {
        Element fromUserNameElement = rootElement.element("FromUserName");
        Element toUserNameElement = rootElement.element("ToUserName");
        Element contentElement = rootElement.element("Content");
        String fromUserName = toUserNameElement.getTextTrim();
        String toUserName = fromUserNameElement.getTextTrim();
        String msg = contentElement.getTextTrim();

        logger.info(Constants.LOGPRIFIX + "fromUserName=" + fromUserName + ",toUserName=" + toUserName + ",msg=" + msg);

        ReplyTextMsg replyTextMsg = new ReplyTextMsg();
        replyTextMsg.setToUserName(toUserName);
        replyTextMsg.setFromUserName(fromUserName);
        replyTextMsg.setMsgType("transfer_customer_service");
        replyTextMsg.setCreateTime(new Date().getTime());
        ModelAndView xmlMAV = new ModelAndView(jaxb2MarshallingView);
        xmlMAV.addObject(replyTextMsg);
        return xmlMAV;
    }

    /**
     * 接受回复图片消息
     *
     * @param rootElement
     * @return
     */
    private ModelAndView receiveImageMsg(Element rootElement) {
        Element fromUserNameElement = rootElement.element("FromUserName");
        Element toUserNameElement = rootElement.element("ToUserName");
        Element mediaIdElement = rootElement.element("MediaId");
        String fromUserName = toUserNameElement.getTextTrim();
        String toUserName = fromUserNameElement.getTextTrim();
        String mediaId = mediaIdElement.getTextTrim();

        logger.info(Constants.LOGPRIFIX + "fromUserName=" + fromUserName + ",toUserName=" + toUserName + ",mediaId=" + mediaId);

        ReplyTextMsg replyTextMsg = new ReplyTextMsg();
        replyTextMsg.setToUserName(toUserName);
        replyTextMsg.setFromUserName(fromUserName);
        //转发给多客服系统
        replyTextMsg.setMsgType("transfer_customer_service");
        replyTextMsg.setCreateTime(new Date().getTime());
        ModelAndView xmlMAV = new ModelAndView(jaxb2MarshallingView);
        xmlMAV.addObject(replyTextMsg);
        return xmlMAV;
    }
}
